import 'package:flutter/material.dart';
import 'package:gold/app/user_provider.dart';
import 'package:gold/base/view_model.dart';
import 'package:gold/http/api_login.dart';
import 'package:gold/http/http.dart';
import 'package:gold/route/xfg_nav.dart';

/// @description:
/// @author tangzhentao
/// @created at 21:12 2025/1/1

class NameViewmodel extends Viewmodel {
  /// 昵称输入相关
  final TextEditingController nameController = TextEditingController();
  final FocusNode nameNode = FocusNode();

  bool _canClick = false;
  /// 是否可点击
  bool get canClick => _canClick;

  NameViewmodel() {
    nameController.text = XfgNav.read<UserProvider>().user.nickname?? '';
  }

  /// 手机号变化
  void phoneChanged(String phone) {
    final click = phone.isNotEmpty;
    if (_canClick != click) {
      _canClick = click;
      notifyListeners();
    }
  }

  /// 提交修改
  Future submit() async {
    final result = await Http().request<int>(ApiLogin.completeProfile,
        data: {'nickname': nameController.text}, loadingText: '加载中...', successText: '修改成功~'
    );
    if (result.success) {
      await XfgNav.read<UserProvider>().updateProfile();
      XfgNav.pop();
    }
  }

  @override
  void dispose() {
    nameNode.dispose();
    nameController.dispose();
    super.dispose();
  }
}
